package stark.app.frame.document;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.smtt.sdk.TbsReaderView;

import stark.app.R;
import stark.common.basic.utils.ToastUtil;

/**
 * <pre>
 *     author : cfans
 *     time   : 2020/11/06
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class QbSDKViewerActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback {

    public static final String PATH = "path";
    public static final String SUFFIX = "suffix";

    public static void startDocumentViewer(Context context,String path, String suffix){
        Intent i = new Intent(context,QbSDKViewerActivity.class);
        i.putExtra(PATH,path);
        i.putExtra(SUFFIX,suffix);
        context.startActivity(i);
    }

    private TbsReaderView mTbsReaderView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RelativeLayout layout = new RelativeLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        layout.setLayoutParams(params);
        setContentView(layout);
        setupReaderView(layout);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mTbsReaderView.onStop();
    }

    private void previewData(){
        Intent i = getIntent();
        String path = i.getStringExtra(PATH);
        String suffix = i.getStringExtra(SUFFIX).replace(".","");

        Bundle b = new Bundle();
        b.putString("filePath", path);
        b.putString("tempPath", Environment.getExternalStorageDirectory() + "/" +"TbsReaderTemp");
        if (mTbsReaderView.preOpen(suffix, false)){
            this.mTbsReaderView.openFile(b);
        }
    }

    private void setupReaderView(RelativeLayout view){
        mTbsReaderView = new TbsReaderView(this,this);
        view.addView(mTbsReaderView);
        mTbsReaderView.post(new Runnable() {
            @Override
            public void run() {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                mTbsReaderView.setLayoutParams(layoutParams);
                previewData();
            }
        });
    }


    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {
        Log.e("QbSdk", ""+integer+" o "+o.toString() +"o1" +o1.toString());

    }
}
